const path = require("path");
const requireFile = require("./requireFile");
const _ = require("lodash");

function getConfig() {
    let config = cache.config;
    if (Object.keys(cache.config).length) {
        return config;
    }
    const cliConfigPath = cache.cliConfigPath;

    const dc = requireFile(`${cache.root}/${cliConfigPath}`);
    const pc = requireFile(`${ cache.projectRoot}/config.hs.js`);

    config = _.merge(dc, pc);
    cache.config = config;
    return config;
};

const cache = {
    cliConfigPath: "config.cli.js",
    config: {},
    root: path.resolve(__dirname, "../../"),
    projectRoot: process.cwd(),
    nmRoot: "",
    init() {
        //project root
        cache.projectRoot = process.cwd();
        //cli root
        cache.root = path.resolve(__dirname, "../../");
        //node modules root
        cache.nmRoot = cache.root;
        cache.config = getConfig();
        return cache;
    }

}

module.exports = cache;